<template>
  <a-form-model
    ref="ruleForm"
    :model="form"
    :rules="rules"
    :label-col="labelCol"
    :wrapper-col="wrapperCol"
    style="top:10px;"
    >
    <a-row>
      <a-col :span="24">
        <a-card :title="$t('m.codegen.label.module_infor_config')">
          <a-row>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.codegen.entity.packagePath')" ref="packagePath" prop="packagePath">
                <a-input v-model="form.packagePath" :placeholder="$t('m.codegen.entity.packagePath_placeholder')"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item ref="moduleName" :label="$t('m.codegen.entity.moduleName')" prop="moduleName">
                <a-input v-model="form.moduleName" :placeholder="$t('m.codegen.entity.moduleName_placeholder')"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.codegen.entity.moduleDesc')">
                <a-input v-model="form.moduleDesc" :placeholder="$t('m.codegen.entity.moduleDesc_placeholder')"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.codegen.entity.authorName')">
                <a-input v-model="form.authorName" :placeholder="$t('m.codegen.entity.authorName_placeholder')"></a-input>
              </a-form-model-item>
            </a-col>

          </a-row>
          <a-row>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.system.user.entity.email')">
                <a-input v-model="form.authorEmail" :placeholder="$t('m.codegen.entity.authorEmail_placeholder')"></a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
      </a-col>

    </a-row>
    <a-row>
      <a-col :span="24">
        <a-card :title="$t('m.codegen.label.back_module_config')">
          <a-row>
            <a-col :span="12">

              <a-form-model-item :label="$t('m.codegen.label.javaPackage')">
                {{javaPackage}}
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.codegen.label.genFile')">
                <a-checkbox v-model="form.genJava" @change="changeGenJava" :defaultChecked="true" value="true">{{$t('m.codegen.label.genFile_java')}}</a-checkbox>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
      </a-col>

    </a-row>
    <a-row>
      <a-col :span="24">
        <a-card :label="$t('m.codegen.label.front_module_config')">
          <a-row>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.codegen.label.front_vue_path')">
                {{vuePath}}
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item :label="$t('m.codegen.label.genFile')">
                <a-checkbox v-model="form.genWeb" @change="changeGenWeb" :defaultChecked="true" value="true">{{$t('m.codegen.label.genFile_web')}}</a-checkbox>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-card>
      </a-col>

    </a-row>

  </a-form-model>
</template>

<script>

  import { getAction, httpAction } from '@/api/manage'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'Step2',
    props: {
      iProps: {
        type: Object,
        default: () => {
        }
      },
      genConfigId:{
        type:String,
        default:""
      }

    },
    data () {
      return {
        form: {
          moduleDesc: '',
          moduleName:'',
          authorName: '',
          authorEmail:'',
          webPath:'',
          packagePath:'com.yuncheng',
          genWeb:true,
          genJava:true,
        },
        labelCol: {
          xs: { span: 22 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 22 },
          sm: { span: 16 },
        },
        rules: {
          moduleName: [{ required: true, message: this.$t('m.eform.formDesign.label.required_message') }],
          packagePath: [{ required: true, message: this.$t('m.eform.formDesign.label.required_message') }],
        },
        url: {
          saveGenConfig: YchConstant.PREFIX_EFORM + '/generator/manage/genConfig/edit',
        }
      }
    },
    methods: {
      prevStep () {
        this.$emit('prevStep')
      },
      nextStep () {
        this.handleOk().then(() => this.$emit('nextStep'))
      },
      save(){
        this.handleOk()
      },
      finish () {
        this.handleOk().then(() => this.$emit('finish'))
      },
      show () {
        let sysGenConfig = this.iProps['sysGenConfig']
        if(sysGenConfig.genWeb=='true' || sysGenConfig.genWeb=='' || sysGenConfig.genWeb==undefined){
          sysGenConfig.genWeb = true
        }
        if(sysGenConfig.genJava=='true' || sysGenConfig.genJava=='' || sysGenConfig.genJava==undefined){
          sysGenConfig.genJava = true
        }
        if(sysGenConfig.packagePath == undefined || sysGenConfig.packagePath==''){
          sysGenConfig.packagePath = 'com.yuncheng'
        }
        if(sysGenConfig.webPath == undefined || sysGenConfig.webPath==''){
          sysGenConfig.webPath = ''
        }

        this.form = Object.assign({}, sysGenConfig)
      },
      handleOk() {
        let that = this
        let promise = new Promise(function (resolve, reject) {
          that.$refs.ruleForm.validate(valid => {
            if (valid) {

              let method = 'put'
              let sysGenConfig = that.iProps['sysGenConfig']
              sysGenConfig.id = that.genConfigId
              that.form.webPath = that.form.moduleName
              Object.assign(sysGenConfig, that.form)
              sysGenConfig.processStep = "3";
              that.$emit('setProp', 'sysGenConfig', sysGenConfig)

              httpAction(that.url.saveGenConfig, sysGenConfig, method).then((res) => {
                if (res.success) {
                  that.messageUtil.success(res.message)
                  resolve(res.message)
                } else {
                  that.messageUtil.warning(res.message)
                  reject(res.message)
                }
              }).finally(() => {
              })
            }
          })
        });
        return promise;
      },
      changeGenWeb(e){
        if(e.target.checked){
          this.form.genWeb = true
        }else{
          this.form.genWeb = false
        }
      },
      changeGenJava(e){
        if(e.target.checked){
          this.form.genJava = true
        }else{
          this.form.genJava = false
        }
      },
    },
    computed: {
      javaPackage () {
        return this.form.moduleName?this.form.packagePath + '.' +this.form.moduleName:this.form.packagePath
      },
      vuePath(){
        return this.form.moduleName?'views/'+this.form.moduleName:'views'
      }
    },
  }
</script>
